﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RuleCode.Table;
using System.Data;

namespace chatblog.usercate
{
    public partial class ChatSetup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof (ChatSetup ));
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                dealrequest();
            }
        }

        private void dealrequest()
        {
            if (AliApi.appUserId == "")
            {
                Response.Redirect("../member/login.aspx?redirecturl=" + "../default.aspx");
            }
        }

        #region　Ajax方法
        
        [AjaxPro .AjaxMethod ]
        public string GetPersonChatRoomStatus()
        {
            string status = "不在线";
            if (AliApi.appUserId != null && AliApi.appUserId != "")
            {
                cbuser user = cbuser.Find("cbuserid='"+AliApi.appUserId+"'");
                if (user.Count > 0)
                {
                    status = (user.chatIsOnLine.Value == RuleCode.Enum.BooleanEnum.Yes) ? "在线" : "不在线";
                }
            }
            return status;
        }

        [AjaxPro .AjaxMethod ]
        public void ForceToUnLine()
        {
            if (AliApi.appUserId != null && AliApi.appUserId != "")
            {
                cbuser user = cbuser.Find("cbuserid='" + AliApi.appUserId + "'");
                if (user.Count > 0)
                {
                    user.chatIsOnLine.Value = RuleCode.Enum.BooleanEnum.No;
                    user.cbChatAddress.Value = "";
                    user.Modify();
                }

                AliApi.chatOpenTime = "";
                cbChatRoom chatRoom = cbChatRoom.Find("chat_from='" + AliApi.appUserId + "'");
                if (chatRoom.Count > 0)
                {
                    chatRoom.Delete("chat_from='" + AliApi.appUserId + "'");
                }
            }
        }  

        [AjaxPro .AjaxMethod ]
        public string GetCommonChatListHTML()
        {
            string html = string.Empty;
            if (AliApi.appUserId != null && AliApi.appUserId != "")
            {
                int count = 0;
                int columnCount = 5;
                string tableHeader = "<table style=\"width:100%\">";
                string tableFooter = "</table>";
                string tableBody = string.Empty;
                string userFilter = "UserID='"+AliApi.appUserId +"'";
                DataRow[] userRows = CommonChatRoomCache.ChatUserTable.Select(userFilter );
                foreach (DataRow userRow in userRows)
                {
                    string roomID = userRow["RoomID"].ToString();
                    string roomFilter = "RoomID='"+roomID +"'";
                    DataRow[] roomRows = CommonChatRoomCache.ChatRoomTable.Select(roomFilter);
                    if (roomRows.Length > 0)
                    {
                        if (count % columnCount == 0)
                            tableBody += "<tr>";
                        tableBody += "<td><input  type =\"checkbox\" id=\"" + roomRows[0]["RoomID"].ToString() + "\" value=\"" + roomRows[0]["RoomType"].ToString() + "\"/>" + roomRows[0]["RoomName"].ToString() + "</td>";
                        if (count % columnCount == (columnCount - 1))
                            tableBody += "</tr>";
                        count++;
                    }
                }
                if (count > 0)
                    html = tableHeader + tableBody + tableFooter;
            }
            return html;
        }

        [AjaxPro .AjaxMethod ]
        public void ForceCommonChatRoomUnline(string roomId)
        {
            if (AliApi.appUserId != null && AliApi.appUserId != string.Empty)
            {
                DataRow[] userRows = CommonChatRoomCache.ChatUserTable.Select("RoomID='" + roomId + "' and UserID='" + AliApi .appUserId  + "'");
                if (userRows.Length > 0)
                {
                    //移除用户                         
                    CommonChatRoomCache.ChatUserTable.Rows.Remove(userRows[0]);
                    DataRow[] otherUserRows = CommonChatRoomCache.ChatUserTable.Select("RoomID='" + roomId + "'");
                    if (otherUserRows.Length <= 0)
                    {
                        //移除聊天室
                        DataRow[] roomRows = CommonChatRoomCache.ChatRoomTable.Select("RoomID='" + roomId + "'");
                        if (roomRows.Length > 0)
                            CommonChatRoomCache.ChatRoomTable.Rows.Remove(roomRows[0]);
                        //移除聊天室对应的聊天记录
                        DataRow[] chatInfoRows = CommonChatRoomCache.ChatInfoTable.Select("RoomID='" + roomId + "'");
                        if (chatInfoRows.Length > 0)
                        {
                            foreach (DataRow chatInfoRow in chatInfoRows)
                                CommonChatRoomCache.ChatInfoTable.Rows.Remove(chatInfoRow);
                        }
                    }
                }
            }
        }
        #endregion
    }
}
